package org.jetnuke.dao.generic;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import net.jforum.JForumExecutionContext;
import net.jforum.util.preferences.SystemGlobals;

import org.jetnuke.dao.PictureUserSettingsDAO;
import org.jetnuke.entities.PictureUserSettings;

public class GenericPictureUserSettingsDAO implements PictureUserSettingsDAO {

	public PictureUserSettings retrieveByPk(int id) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("PictureUserSettingsModel.selectByID"));
		p.setInt(1, id);
		
		ResultSet rs = p.executeQuery();
		PictureUserSettings s = new PictureUserSettings();
		
		if (rs.next()) {
			fillPictureUserSettingsFromResultSet(s, rs);
		}
		
		rs.close();
		p.close();

		return s;
	}

	private void fillPictureUserSettingsFromResultSet(PictureUserSettings s, ResultSet rs)throws Exception {
		
		s.setDESCRIPTION(rs.getString("DESCRIPTION"));
		s.setTHEME(rs.getInt("THEME"));
		s.setTITLE(rs.getString("TITLE"));
		s.setID(rs.getInt(("ID")));
	}

	public void update(PictureUserSettings bs) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("PictureUserSettingsModel.update"));
		p.setString(1,bs.getDESCRIPTION());
		p.setInt(2, bs.getTHEME());
		p.setString(3,bs.getTITLE());
		
		p.setInt(4,bs.getID());
		p.executeUpdate();
		p.close();

	}

	public void insert(PictureUserSettings bs) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("PictureUserSettingsModel.insert"));
		p.setString(1,bs.getDESCRIPTION());
		p.setInt(2, bs.getTHEME());
		p.setString(3,bs.getTITLE());
		p.setInt(4,bs.getID());
		p.executeUpdate();
		p.close();

	}

	public int countAll() throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("PictureUserSettingsModel.countAll"));
		ResultSet rs=p.executeQuery();
		rs.next();
		int ret=rs.getInt(1);
		rs.close();
		p.close();
		return ret;
	}

	public List get(int start, int limit) throws Exception {
		PreparedStatement p= JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("PictureUserSettingsModel.get"));
		p.setInt(1,start);
		p.setInt(2,limit);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;
	}

	protected List processSelectAll(ResultSet rs) throws Exception{
		List list = new ArrayList();
		while (rs.next()) {
			PictureUserSettings u = new PictureUserSettings();
			fillPictureUserSettingsFromResultSet(u,rs);			
			list.add(u);
		}
		return list;
	}

}
